package org.egedede.gravity.model;

import java.util.Iterator;

import org.egedede.simulation.model.Model;

import com.google.common.collect.Lists;

public class GravityModelIterator implements Iterator<Model> {

	private final int startVx;
	private final int endVx;
	private final int stepVx;
	private final int startVy;
	private final int endVy;
	private final int stepVy;

	private final int constant;
	private final int x, y;
	private final int mass, massRef;

	private int currVx, currVy;

	private int step = 0;

	public GravityModelIterator(int startVx, int endVx, int stepVx,
			int startVy, int endVy, int stepVy, int constant, int x, int y,
			int mass, int massRef) {
		this.startVx = startVx;
		this.endVx = endVx;
		this.stepVx = stepVx;
		this.startVy = startVy;
		this.endVy = endVy;
		this.stepVy = stepVy;
		this.constant = constant;
		this.x = x;
		this.y = y;
		this.mass = mass;
		this.massRef = massRef;

		currVx = startVx;
		currVy = startVy;
	}

	@Override
	public boolean hasNext() {
		return currVx < endVx && currVy < endVy;
	}

	@Override
	public Model next() {
		Body ref = new Body(0, 0, massRef, "Reference");
		Body tested = new Body(x, y, mass, "Iteration " + step++);
		tested.setVx(currVx);
		tested.setVy(currVy);
		GravityModel model = new GravityModel();
		GravitySystem system = new GravitySystem();
		system.setConstant(constant);
		system.setBodies(Lists.newArrayList(ref,tested));
		model.setSystem(system );

		// increase now counters
		currVx=currVx+stepVx;
		if(currVx>endVx){
			currVx=startVx;
			currVy+=stepVy;
		}
		return model;
	}

	@Override
	public void remove() {
		throw new UnsupportedOperationException(
				"Not possibleto remove fromsuch an iterator");
	}

}
